#import "ALTextElementColumnEntry.h"
#import "ALElement.h"
#import "NSObject+BlockObservation.h"


@implementation ALTextElementColumnEntry

@synthesize valueView;


-(void)awakeFromNib {
	[super awakeFromNib];
    NSNotificationCenter *noteCenter = [NSNotificationCenter defaultCenter];
	[noteCenter addObserver:self selector:@selector(viewFrameDidChange:) name:NSViewFrameDidChangeNotification object:[self view]];
}


#pragma mark -
#pragma mark Notifications

-(void)viewFrameDidChange:(NSNotification *)note {
	// This monitors changes in the view's frame due to font size changes (primary elements are larger than secondary elements).  Label and value views are adjusted to fit since text fields won't autoresize vertically
	CGFloat viewHeight = [[self view] frame].size.height;
	
	NSRect valueFrame = [[self valueView] frame];
	valueFrame.size.height = viewHeight;
	[[self valueView] setFrame:valueFrame];
	
	NSRect labelFrame = [[self labelView] frame];
	labelFrame.size.height = viewHeight;
	[[self labelView] setFrame:labelFrame];
}


#pragma mark -
#pragma mark Column entry overrides

-(void)columnEntryDidBeginEditingAtPoint:(NSPoint)clickPoint {
	[super columnEntryDidBeginEditingAtPoint:clickPoint];
	[[self valueView] setEditable:YES];
}

-(void)columnEntryDidEndEditing {
	[[self valueView] setEditable:NO];
	NSWindow *window = [[self valueView] window];
	[window invalidateCursorRectsForView:[self valueView]];
	[super columnEntryDidEndEditing];
}

-(void)columnEntryWasAddedToColumnView {
	[super columnEntryWasAddedToColumnView];
    NSNotificationCenter *noteCenter = [NSNotificationCenter defaultCenter];
	[noteCenter addObserver:self selector:@selector(viewFrameDidChange:) name:NSViewFrameDidChangeNotification object:[self view]];
}

//-(CGFloat)preferredHeightForWidth:(CGFloat)width {
//	BOOL elementIsPrimary = [self elementIsPrimary];
//	CGFloat fontSize = elementIsPrimary ? ALElementFontSizePrimary : ALElementFontSizeSecondary;
//	CGFloat viewHeight = fontSize + [self textPadding];
//	return viewHeight;
//}

-(NSView *)willForwardHitTestAtPoint:(NSPoint)aPoint toSubview:(NSView *)subview {
	if (subview == [self valueView] && [[self valueView] isEditable])
		return subview;
	NSText *fieldEditor = [[[self valueView] window] fieldEditor:NO forObject:nil];
	if (subview == fieldEditor && (id)[fieldEditor delegate] == [self valueView])
		return subview;
	return [self columnView];
}

#pragma mark -
#pragma mark Value view delegate

- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)command {
	if (command == @selector(insertNewline:)) {
		[[self columnView] stopEditing];
		NSWindow *window = [[self columnView] window];
		[window makeFirstResponder:[self columnView]];
		return YES;
	}
	if (command != @selector(cancelOperation:)) 
		return NO;
	[self discardEditing];
	[[self valueView] hideEditModeBezel];
	[[self columnView] stopEditing];
	NSWindow *window = [[self columnView] window];
	[window makeFirstResponder:[self columnView]];
	return YES;
}

@end
